﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MYNAMESPACE;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Net.Mail;

namespace final_project
{
    public partial class forget : Form
    {
        private connect_db con;
        public SmtpClient client = new SmtpClient();
        public MailMessage msg = new MailMessage();
        public System.Net.NetworkCredential smpt =
            new System.Net.NetworkCredential("hima4105@gmail.com","0562192218");
        public forget()
        {
            InitializeComponent();
        }

        public void SendEmail(string sendTo, string sendFrom, string subject, string body) {
            try
            {
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.Credentials = smpt;
                client.EnableSsl = true;

                MailAddress to = new MailAddress(sendTo);
                MailAddress from = new MailAddress(sendFrom);

                msg.Subject = subject;
                msg.Body = body;
                msg.From = from;
                msg.To.Add (to);

                client.Send(msg);



            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"error");
            
            }
                        
        }
        private void button1_Click(object sender, EventArgs e)
        {

            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 
            
            if (ID.Text.Length == 0) { MessageBox.Show("you must have value"); }
            else
            {
                user US = new user();
                US.setnational_id(Int32.Parse(ID.Text.Trim()));
                con = new connect_db();
                con.sqlquery("select pass from userdata where id=(@namep)");
                con.cmd.Parameters.Add("@namep", ID.Text.Trim());
                int count = 0;
                foreach (DataRow dr in con.QueryEx().Rows)
                {

                    count = count + 1;
                }
                
                if (count == 1)
                {
                    con.sqlquery("select pass from userdata where id=(@namep)");
                    con.cmd.Parameters.Add("@namep", ID.Text.Trim());
                    
                    foreach (DataRow dr in con.QueryEx().Rows)
                    {

                    MessageBox.Show("the password is " + dr[0].ToString());
                    }
                }
                    
                else { 
                MessageBox.Show("not exist");

                
                }
               
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 
            
            if (ID.Text.Length == 0) { MessageBox.Show("you must have value"); }
            else
            {
                user US = new user();
                US.setnational_id(Int32.Parse(ID.Text.Trim()));
                con = new connect_db();
                con.sqlquery("select pass from userdata where id=(@namep)");
                con.cmd.Parameters.Add("@namep", ID.Text.Trim());
                int count = 0;
                foreach (DataRow dr in con.QueryEx().Rows)
                {

                    count = count + 1;
                }

                if (count == 1)
                {
                    con.sqlquery("select pass from userdata where id=(@namep)");
                    con.cmd.Parameters.Add("@namep", ID.Text.Trim());

                    foreach (DataRow dr in con.QueryEx().Rows)
                    {




                        con.sqlquery("select mail from userdata where id=(@namep)");
                        con.cmd.Parameters.Add("@namep", ID.Text.Trim());
                        foreach (DataRow dr2 in con.QueryEx().Rows)
                        {


                            SendEmail(dr2[0].ToString() + "@gmail.com", "hima4105@gmail.com", "Bank System", "pass is  " + dr[0].ToString() + "Your id is   " + ID.Text);
                            this.Hide();
                            User f1 = new User();
                            f1.ShowDialog();
                        }
                    }


                }
                else
                {
                    MessageBox.Show("not exist");
                }
                
               
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            char cb = e.KeyChar;
            if (!char.IsDigit(cb) && cb != 8)
            {

                e.Handled = true;

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();
            User f1 = new User();
            f1.ShowDialog();
        }

        private void forget_Load(object sender, EventArgs e)
        {

        }

        private void ID_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(ID.Text))
            {

                e.Cancel = true;
                ID.Focus();
                errorProvider1.SetError(ID, "ID Must Be Entered !");

            }

            else if (!Regex.IsMatch(ID.Text, @"^\d{4}$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(ID, "Valid ID 4 Number");

            }



            else if (Regex.IsMatch(ID.Text, @"^\d{4}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(ID, "");

            }
            else { }
        }
    }
}
